<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Array,
      required: true
    },
    title: {
      type: String,
      default: '代理商设备统计'
    },
    xColor: {
      type: Boolean,
      default: true
    },
    yColor: {
      type: String,
      default: '#c7b198'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        let value = []
        let name = []
        if (val.length != 0) {
          value = val.map(res => res.num)
          name = val.map(res => res.username)
        }
        this.setOptions(value, name,this.xColor)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      // this.setOptions(this.chartData)
    },
    setOptions(value, names,type) {
      this.chart.setOption({
        backgroundColor: 'rgba(0,0,0,0)',
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          },
          formatter: "{b} <br/> {a1}: {c} "
        },
        legend: {
          data: ['line', this.title],
          textStyle: {
            color: '#ccc'
          }
        },
        grid: {
          left: 10,
          right: 20,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        xAxis: {
          data: names,
          axisLine: {
            lineStyle: {
              color: '#ccc'
            }
          },
          splitArea: {
            show: false
          },
          axisLine: {
            show: false,
            lineStyle: {
              color: '#868c96'
            }
          },
          axisTick: {
            show: false
          }
        },
        yAxis: {
          minInterval: 1,
          splitLine: { show: false },
          axisLine: {
            lineStyle: {
              color: '#ccc'
            }
          },
          splitArea: {
            show: false
          },
          axisLine: {
            show: false,
            lineStyle: {
              color: '#868c96'
            }
          },
          axisTick: {
            show: false
          }
        },
        axisLabel: {
          interval: 0,
          formatter: function (value) {
            return value.length > 4 ? `${value.substring(0, 4)}...` : value
          }
        },
        series: [{
          name: 'line',
          type: 'line',
          showAllSymbol: true,
          symbol: 'emptyCircle',
          symbolSize: 10,
          data: value,
          itemStyle: {
            color: this.yColor,
          },
        }, {
          name: this.title,
          type: 'bar',
          barWidth: 10,
          itemStyle: {
            barBorderRadius: 0,
            normal: {
              label: {
                show: true,		//开启显示
                position: 'top',	//在上方显示
                textStyle: {	    //数值样式
                  color: '#ffc72b',
                  fontSize: 10
                }
              },
              color: function (params) {
                var colorList = ['#ff2600', '#ffc000', '#00ad4e', '#0073c2', '#165868', '#e76f00', '#316194', '#723761', '#00b2f1', '#4d6022', '#4b83bf', '#f9c813', '#0176c0'];
                if(type) {
                  return '#5154FF'
                }else {
                  return colorList[params.dataIndex]
                }
                
              },
            }
          },
          data: value,
          animationDuration: 3800,
          animationEasing: 'cubicInOut'
        }]

      })
    }
  }
}
</script>
